<template>
  <div>
    <el-card>
      <span>余志峰</span>
      <el-row :gutter="21">
        <el-col :span="5">
          <!-- 搜索输入框 -->
          <el-input
            v-model.trim="input"
            placeholder="根据用户名搜索"
            @clear="gettableData"
          ></el-input>
        </el-col>
        <el-col :span="16">
          <!-- 按钮 -->
          <el-button @click="clearbtn">清除</el-button>
          <el-button type="primary" @click="serachbtn">搜索</el-button>
        </el-col>
        <!-- 新增权限按钮 -->
        <el-col :span="2">
          <el-button type="success" icon="el-icon-edit" @click="addNewDialog"
            >新增权限组</el-button
          >
        </el-col>
      </el-row>
      <!-- 提示标签语 -->
      <el-alert :title="countext" type="info" show-icon> </el-alert>
      <!-- table 页面 -->
      <el-table :data="tableData" style="width: 100%">
        <el-table-column type="selection" width="150"> </el-table-column>
        <el-table-column prop="title" label="用户名" width="400">
        </el-table-column>
        <el-table-column prop="create_date" label="日期" sortable width="500">
        </el-table-column>
        <el-table-column label="操作" width="120">
          <!-- 编辑按钮 -->
          <template slot-scope="scope">
            <el-button
              type="primary"
              icon="el-icon-edit"
              circle
              plain
              @click="etidpermissions(scope.row)"
            >
            </el-button>
            <!-- 删除按钮 -->
            <el-button
              v-if="scope.row.id !== 2"
              type="danger"
              icon="el-icon-delete"
              circle
              plain
              @click="removePerDelete(scope.row)"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 底部分页 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="Number(page)"
        :page-sizes="[5,10,15]"
        :page-size="Number(pagesize)"
        layout="  prev, pager, next,sizes,jumper"
        :total="10"
        class="flooter"
      >
      </el-pagination>
      <!-- 弹框 -->
      <Permissions
        :text="text"
        :pageTitle="pageTitle"
        @handleCloseModal="dialogFormH"
        ref="add"
        :ruleInline="permissionsRule"
        @handleAdd="confirmAdded"
        @newDataes="confirmAdded"
      ></Permissions>
    </el-card>
  </div>
</template>

<script>
import moment from 'moment'
import Permissions from '@/module-manage/components/permissions-add.vue'
import { list, remove } from '@/api/base/permissions'

export default {
  data () {
    return {
      input: '',
      text: '',
      pageTitle: '权限组',
      querinfo: {
        page: '',
        pagesize: 5,
        title: ''
      },
      pages: 1,
      pagesize: '',
      page: '',
      counts: 0,
      countext: '',
      editForm: {
        id: 0,
        create_date: '',
        title: '',
        permissions: []
      },
      permissionsRule: {
        title: [
          { required: true, messages: '请输入用户名', trigger: 'blur' },
          { min: 3, max: 6, message: '请输入3～6字符', trigger: 'blur' }
        ]
      },
      searchbtn: '',
      tableData: [],
      addnewDialog: false,
      addForm: {
        title: ''
        // permissions: 'res.data'
      },
      addnewDialogRules: {
        name: {
          required: true,
          message: '输入不能为空',
          trigger: 'blur'
        }
      },
      treeData: [],
      defkeyss: [],
      defaultProps: {
        children: 'childs',
        label: 'title'
      }
    }
  },

  created () {
    this.gettableData()
  },
  computed: {},
  components: {
    Permissions
  },
  methods: {
    // 新增用户
    addNewDialog () {
      this.text = '新增'
      this.$refs.add.dialogFormV()
      this.$refs.add.handleResetForm()
    },
    // 关闭清空事件

    dialogFormH () {
      this.$refs.add.dialogFormH()
    },
    confirmAdded () {
      // console.log(1)
      this.gettableData()
    },
    // 刷新数据
    async gettableData () {
      const res = await list(this.querinfo)
      console.log(res)
      this.tableData = res.data.list
      // this.gettableData()
      this.counts = res.data.counts
      this.countext = `共${this.counts}数据`
      res.data.list.forEach(item => {
        item.create_date = moment(item.create_date).format('YYYY-MM-DD')
      })
    },
    // 底部分页
    handleSizeChange (newSize) {
      this.querinfo.pagesize = newSize
      this.gettableData()
    },
    handleCurrentChange (newPage) {
      this.querinfo.page = newPage
      this.gettableData()
    },
    // 编辑按钮
    etidpermissions (row) {
      this.text = '修改'
      this.$refs.add.dialogFormV() // 调用显示框
      this.$refs.add.hanldeEditForm(row.id) // 给子组件传入id值
    },
    // 清除按钮
    clearbtn () {
      this.input = ''
      this.gettableData()
      return this.$message.success('已清除所有内容！')
    },
    // 搜索按钮
    async serachbtn () {
      if (this.input.length === 0) return this.$message.error('不能为空')
      const res = await list({
        page: '',
        pagesize: 5,
        title: this.input
      })
      console.log(res)
      this.tableData = res.data.list
      this.counts = res.data.counts
      this.countext = `共${this.counts}数据`
      res.data.list.forEach(item => {
        item.create_date = moment(item.create_date).format('YYYY-MM-DD')
      })
      return this.$message.success('搜索成功！')
    },
    // 删除按钮
    async removePerDelete (row) {
      const configDelete = await this.$confirm(
        '此操作将永久删除该文件, 是否继续?',
        '提示',
        {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }
      ).catch(err => err)
      if (configDelete !== 'confirm') {
        return this.$message.info('您取消操作')
      }
      try {
        const { data } = await remove(row)
        console.log(data)
        this.gettableData()
        this.$message.success('删除成功')
      } catch (error) {
        this.$message.error('删除失败')
      }
    },
    // 监听用重置时间
    addnewDialogClose () {
      this.$refs.ruleFormRef.resetFields()
    }
  }
}
</script>

<style scoped lang="less">
.el-card {
  padding: 5px;
  margin: 20px;
}

.el-alert {
  margin: 15px 0 20px;
}
.flooter {
  margin-top: 20px;
}
.inputQx {
  width: 300px;
}
</style>
